/**
 * 单例装配器 无论new多少次都是同一个对象
 * @param class 
 * @returns construct
 */
 export const SINGLETON_KEY = Symbol();
 export type ISingleton<T extends new (...args: any[]) => any> = T & {
   [SINGLETON_KEY]: T extends new (...args: any[]) => infer I ? I : never
 };

 
export const Singleton = <T extends new (...args: any[]) => any>(type: T) =>
  new Proxy(type, {
    construct(target: ISingleton<T>, argsList, newTarget) {
      // skip the proxy for children of our target class
      if (target.prototype !== newTarget.prototype) {
        return Reflect.construct(target, argsList, newTarget);
      }
      if (!target[SINGLETON_KEY]) {
        target[SINGLETON_KEY] = Reflect.construct(target, argsList, newTarget);
      }
      return target[SINGLETON_KEY];
    },
  });
